package com.soul.dj.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.*;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.soul.dj.R;


/**
 * h5加载页
 * @author SOUL 2016/07/07
 */
public class WebActivity extends BaseActivity {

	private TextView finish;
	private TextView titleTxt;

	private WebView webView;
	private ProgressBar progressBar;

	private String url;

	@Override
	protected void initView() {
		setContentView(R.layout.web_activity);
		url = getIntent().getStringExtra("url");

		finish = (TextView) findViewById(R.id.finish);
		titleTxt = (TextView) findViewById(R.id.title);

		finish.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				if (webView.canGoBack()) {
					webView.goBack(); // goBack()表示返回WebView的上一页面
				} else {

					finish();
				}
			}
		});

		webView = (WebView) findViewById(R.id.web);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		progressBar.setMax(100);

		WebSettings settings = webView.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setUseWideViewPort(true);
		settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		// settings.setLoadWithOverviewMode(true);

		webView.setWebViewClient(new WebViewClient() {

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// Uri uri=Uri.parse(url);
				// if(uri.getScheme().equals("m")&&uri.getHost().equals("payDetail")){
				// String orderNo = uri.getQueryParameter("order_id");
				// Bundle d = new Bundle();
				// d.putString("order_no", orderNo);
				//
				// }else{
				view.loadUrl(url);
				// }
				return true;
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				titleTxt.setText("加载中...");
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				String title = view.getTitle();
				if(title.length()>10){
					title = title.substring(0,9)+"…";
				}
				titleTxt.setText(title);
			}

			@Override
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				handler.proceed();
			}

		});

		webView.setDownloadListener(new DownloadListener() {

			@Override
			public void onDownloadStart(String url, String userAgent,
					String contentDisposition, String mimetype,
					long contentLength) {
				Uri uri = Uri.parse(url);
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		});

		webView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				super.onProgressChanged(view, newProgress);
				if (newProgress == 100) {
					progressBar.setVisibility(View.INVISIBLE);
				} else {
					progressBar.setVisibility(View.VISIBLE);
					progressBar.setProgress(newProgress);
				}
			}
		});
	}

	@Override
	public void onResume() {
		super.onResume();
		webView.loadUrl(url);
	}

	public void refesh() {
		if (webView != null) {
			webView.reload();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
			webView.goBack(); // goBack()表示返回WebView的上一页面
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
